package edu.sctu.xuping.month05.day20150514;

import java.util.*;

/**
 * Created by user16 on 2015/5/14.
 */
public class ListCourse {
    public List CourseInformation;//构造一个容器存放课程信息

    public ListCourse() {
        this.CourseInformation = new ArrayList<>();
    }

    public void addCourse() {
        //用add方法添加课程

        Course course1 = new Course(1, "数据结构");
        CourseInformation.add(course1);
        Course temp = (Course) CourseInformation.get(0);
        System.out.println("添加了课程：" + temp.id + ":" + temp.name);
        //用addAll的方法添加课程
        Course[] courseArray = {new Course(2, "大学英语"), new Course(3, "离散数学")};
        CourseInformation.addAll(Arrays.asList(courseArray));
        Course temp1 = (Course) CourseInformation.get(1);
        Course temp2 = (Course) CourseInformation.get(2);
        System.out.println("添加了课程：" + temp1.id + temp1.name + " " + temp2.id + temp2.name);

        Course[] courseArray1 = {new Course(4, "心理学原理"), new Course(5, "java设计基础")};
        CourseInformation.addAll(Arrays.asList(courseArray1));
        Course temp3 = (Course) CourseInformation.get(3);
        Course temp4 = (Course) CourseInformation.get(4);
        System.out.println("添加了另外的课程：" + temp3.id + ":" + temp3.name + " " + temp4.id + ":" + temp4.name);

    }

    public void courseQuery() {
        System.out.println("运用for循环查询添加的所有课程：");
        //int size=CourseInformation.size();
        for (int i = 0; i <= CourseInformation.size(); i++) {
            Course cr1 = (Course) CourseInformation.get(i);
            System.out.println(cr1.id + ":" + cr1.name);
        }
    }


    public void testForEach() {
        System.out.println("运用for each的方法遍历List查询添加的所有课程：");
        for (Object obj : CourseInformation) {
            Course cr2 = (Course) obj;
            System.out.println(cr2.id + ":" + cr2.name);

        }
    }

    public void testIterator() {
        System.out.println("运用Itrator的方法遍历List查询所有的课程：");
        Iterator it = CourseInformation.iterator();
        while (it.hasNext()) {
            Course cr3 = (Course) it.next();
            System.out.println(cr3.id + ":" + cr3.name);
        }
    }

    public void testModify() {
        System.out.println("修改List中的元素：");
        Course cr4 = (Course) CourseInformation.get(1);
        System.out.println("修改前的课程：" + cr4.id + ":" + cr4.name);
        CourseInformation.set(1, "大学体育");
        Course cr5 = (Course) CourseInformation.get(1);
        System.out.println("修改后的课程：" + cr5.id + ":" + cr5.name);
    }

    public void testRemove() {
        System.out.println("删除List中的元素：");
        System.out.println("删除前全部课程为：");
        int size = CourseInformation.size();
        for (int i = 0; i <= size; i++) {
            Course cr6 = (Course) CourseInformation.get(i);
            System.out.println(cr6.id + ":" + cr6.name);
        }

        CourseInformation.remove(1);
        Course[] courses = {(Course) CourseInformation.get(2), (Course) CourseInformation.get(3)};
        CourseInformation.removeAll(Arrays.asList(courses));

        System.out.println("删除后的课程为：");
        for (Object obj : CourseInformation) {
            Course cr7 = (Course) obj;
            System.out.println(cr7.id + ":" + cr7.name);
        }
    }


}
